/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 22/11/2009, 15:41:22
 */
package grafico;

import acoes.DadosAcao;
import agentes.Investidor;
import agentes.Prospectador;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import normas.NormaPalpiteErrado;
import reputacao.DadosReputacaoIndividual;
import reputacao.DadosReputacaoOntologica;
import reputacao.ReputacaoIndividual;
import reputacao.ReputacaoOntologica;
import reputacao.ReputacaoSocial;
import util.Diversas;
import util.Palpite;

/**
 *
 * @author marapao
 */
public class Principal extends javax.swing.JFrame {

    String reputacaoPro = "Confiável";//reputação do prospeecctador analizadda nesse ambiente
    String reputacaoInv = "Bom testemunho";//reputação do investidor analizadda nesse ambiente
    List<String> acoes = null;//Lista com as ações
    List<Prospectador> prospectadores = null;//Lista que armazenaa os agentesp propsectadores
    List<Investidor> investidores = null;//Lista que armazena os agentes investidores
    List<Palpite> auxRep = new ArrayList<Palpite>();//Lista que arazena o palpique que foi dado a cada rodada
    //utilizada para calcular a reputação em caso de aceto ou erro
    double reputacaoPadrao = 0.8;//reputação de inicialização dos agentes
    long tempoInicial;//tempo inicial de simulação
    int criterioPadrao = 1;//critério que começa o agente prospectador
    double capital = 10000;//dinheeiro inicial de um agente investidor
    private ReputacaoIndividual repInd;//reputaçao individual dos agentes
    private ReputacaoSocial repSoc;//reputação social dos agentes
    private ReputacaoOntologica repOnt;//reputação social dos agentes
    private NormaPalpiteErrado normaPalpite;//objeto responsavel por julgar um agente que infringe a norma dando um  palpite erraddo.
    private static String cods[] = {//códigos de ações existentes

        "QVQP3B", "ABYA3", "AVIL3", "ACGU3", "AELP3", "AESL3", "AESL4", "GETI3", "GETI4", "AFLU3",
        "AFLU5", "AGIN3", "RPAD3", "RPAD5", "RPAD6", "ALLL3", "ALLL4", "TMAC3B", "TMAC5B", "TMAC6B",
        "TMAC7B", "TMAC8B", "TMAC11B", "ABNB3", "AMCE3", "AMIL3", "CBEE3", "AMPI3", "ANDG3B", "ANDG4B",
        "CANT4B", "IRON3", "AEDU4", "ARCZ3", "ARCZ5", "ARCZ6", "VDNP3B", "ARLA3", "ARLA4", "AZEV3",
        "AZEV4", "BTOW3", "BAHI3", "BAHI4", "BEES3", "BEES4", "BDLL3", "BDLL4", "BTTL3", "BTTL4",
        "BALM3", "BALM4", "ABCB4", "BRIV3", "BRIV4", "BAZA3", "BBDC3", "BBDC4", "BBAS3", "CZRS4",
        "DAYC4", "BSCT3", "BSCT5", "BSCT6", "BGIP3", "BGIP4", "BPAR3", "BPIA3", "BRSR3", "BRSR5",
        "BRSR6", "BICB3", "BICB4", "IDVL4", "ITAU3", "ITAU4", "BMIN3", "BMIN4", "BMEB3", "BMEB4",
        "BNBR3", "BNBR4", "BNCA3", "BPNM4", "PINE4", "SANB3", "SANB4", "SFSA4", "PBEL3B", "BEMA3",
        "BMKS3", "BIOM3", "BIOM4", "BVMF3", "BOBR4", "BNPA3B", "STRP4", "BRML3", "BRAP3", "BRAP4",
        "BISA3", "BBRK3", "ECOD3", "BRTP3", "BRTP4", "BRTO3", "BRTO4", "AGRO3", "BRKM3", "BRKM5",
        "BRKM6", "BMTO3", "BMTO4", "BSLI4", "BUET3", "BUET4", "CAFE3", "CAFE4", "CAIA3B", "CCIM3",
        "CAMB4", "CPTP3B", "CRBM3", "CRBM5", "CRBM7", "CELM3", "RANI3", "RANI4", "MAPT3", "MAPT4",
        "CTAP3", "CTMI3", "ELET3", "ELET5", "ELET6", "CLSC3", "CLSC5", "CLSC6", "CELP5", "CELP6",
        "CELP7", "CMGR3", "CMGR4", "CCHI3", "CCHI4", "CESP3", "CESP5", "CESP6", "CBAG3", "AMBV3",
        "AMBV4", "PCAR3", "PCAR4", "CIQU3", "CIQU4", "CASN3", "CASN4", "GPAR3", "CCRO3", "CEGR3",
        "IMBI3", "IMBI4", "CEEB3", "CEEB5", "CEBR3", "CEBR5", "CEBR6", "CMIG3", "CMIG4", "CEPE3",
        "CEPE5", "CEPE6", "COCE3", "COCE5", "COCE6", "ENMA3B", "ENMA6B", "CSRN3", "CSRN5", "CSRN6",
        "CEED3B", "CEED4B", "EEEL3B", "EEEL4B", "FESA3", "FESA4", "CEDO3", "CEDO4", "CGAS3", "CGAS5",
        "HBTS5", "HGTX3", "IGUA3", "IGUA5", "IGUA6", "SCLO3", "SCLO4", "LECO4", "MSPA3", "MSPA4",
        "CPLE3", "CPLE5", "CPLE6", "PEAB3", "PEAB4", "PRVI3", "SBSP3", "CSMG3", "SAPR4", "CSAB3",
        "CSAB4", "CSNA3", "CTNM3", "CTNM4", "CTSA3", "CTSA4", "CTSA8", "FGUI3", "FGUI4", "VALE3",
        "VALE5", "GAFP3", "GAFP4", "CBMA3", "CBMA4", "CPNY3", "OPRT3B", "CNFB3", "CNFB4", "BRGE3",
        "BRGE5", "BRGE6", "BRGE7", "BRGE8", "BRGE11", "BRGE12", "CALI3", "CALI4", "COBE5B", "COBE6B",
        "LIXC3", "LIXC4", "SULT3", "SULT4", "TEND3", "CTAX3", "CTAX4", "CORR4", "CSAN3", "CPFE3",
        "CRDE3", "CREM3", "CARD3", "TRPL3", "TRPL4", "CYRE3", "CCPR3", "DHBI3", "DHBI4", "DASA3",
        "PNVL3", "PNVL4", "DXTG4", "DOCA3", "DOCA4", "DOHL3", "DOHL4", "DROG3", "DTCY3", "GEPA3",
        "GEPA4", "DURA3", "DURA4", "ENBR3", "EALT3", "EALT4", "ELEK3", "ELEK4", "EKTR3", "EKTR4",
        "ELPL3", "ELPL5", "ELPL6", "ELUM3", "ELUM4", "EMAE4", "EMBR3", "EBTP3", "EBTP4", "ECPR3",
        "ECPR4", "ENGI3", "ENGI4", "EQTL3", "ESTC3", "ETER3", "EUCA3", "EUCA4", "EVEN3", "BAUH4",
        "EZTC3", "FTRX3", "FTRX4", "FFTL3", "FFTL4", "FHER3", "SJOS3", "SJOS4", "FAGO3B", "FBMC3",
        "FBMC4", "CRIV3", "CRIV4", "FLYN3B", "FJTA3", "FJTA4", "FRAS3", "FRAS4", "FTRT3B", "GFSA3",
        "GAZO4", "GSHP3", "GGBR3", "GGBR4", "GLOB3", "GOLL4", "GPCP3", "IGBR3", "CGRA3", "CGRA4",
        "GRND3", "GTDP3B", "GTDP4B", "GUAR3", "GUAR4", "GVTT3", "HAGA3", "HAGA4", "HBOR3", "HETA3",
        "HETA4", "HOOT4", "HYPE3", "IDNT3", "IGTA3", "JBDU3", "JBDU4", "ROMI3", "IENG3", "IENG5",
        "INEP3", "INEP4", "INET3", "INPR3", "IVTT3", "FIGE3", "FIGE4", "IVTL3B", "MYPK3", "ITSA3",
        "ITSA4", "ITEC3", "JBSS3", "MLFT3", "MLFT4", "JHSF3", "JFEN3", "JOPA3", "JOPA4", "CTKA3",
        "CTKA4", "KEPL3", "KLBN3", "KLBN4", "KSSA3", "LFFE3", "LFFE4", "LARK4", "LLIS3", "PITI3",
        "PITI4", "BITY3B", "LIPR3", "LIGT3", "LTEL3B", "LGLO4", "LLXL3", "RENT3", "LOGN3", "LAME3",
        "LAME4", "LHER3", "LHER4", "LREN3", "SPRT3B", "LPSB3", "LUPA3", "RHDS3", "MDIA3", "MAGG3",
        "LEVE3", "LEVE4", "MGEL3", "MGEL4", "ESTR3", "ESTR4", "CTPC3", "POMO3", "POMO4", "MRFG3",
        "MARI3", "MRSL3", "MRSL4", "MEDI3", "MLPA4", "MEND3", "MEND5", "MEND6", "MERC3", "MERC4",
        "FRIO3", "MTIG3", "MTIG4", "DUQE3", "DUQE4", "GOAU3", "GOAU4", "RSUL4", "MTSA4", "TIBR3",
        "TIBR5", "TIBR6", "MMAQ4", "BEEF3", "MNPR3", "MMXM3", "MOAR3", "MPXE3", "MRSA3B", "MRVE3",
        "MULT3", "MNDL3", "MNDL4", "NAFG3", "NAFG4", "NATU3", "NETC3", "NETC4", "NEWT3B", "NORD3",
        "NOVA3B", "NOVA4B", "NUTR3M", "OHLB3", "ODPV3", "OPOE3B", "OGXP3", "OPSE3B", "PATI3", "PATI4",
        "PRBC4", "PMAM3", "PMAM4", "LCSA3", "PDGR3", "PRGA3", "PEFX3", "PEFX5", "PETR3", "PETR4",
        "PTPA3", "PTPA4", "PQUN3", "PQUN4", "PTNT3", "PTNT4", "PLAS3", "PSSA3", "PTBL3", "POSI3",
        "PFRM3", "PNOR5", "PNOR6", "QGNP3B", "QGNP4B", "SZPQ4", "RAPT3", "RAPT4", "RSIP3", "RSIP4",
        "RBRA3B", "RBCS3B", "RCSL3", "RCSL4", "REDE3", "REDE4", "RDCD3", "RPMG3", "RPMG4", "RNAR3",
        "RNPT3", "RNPT4", "OPRE3B", "REEM4", "RDNI3", "RSID3", "VAGV3", "VAGV4", "VGOR4", "SDIA3",
        "SDIA4", "FCAP3", "FCAP4", "SNSY3", "SNSY5", "SNSY6", "STBP3", "SCAR3", "SMTO3", "ALPA3",
        "ALPA4", "AHEB3", "AHEB5", "AHEB6", "SLED3", "SLED4", "SATI3", "SHUL4", "SEMP3", "SGEN3",
        "SGEN4", "APTI4", "SLCE3", "SOND3", "SOND5", "SOND6", "CRUZ3", "SPRI3", "SPRI5", "SPRI6",
        "SGPS3", "NEMO4", "SUZB5", "SUZB6", "TAMM3", "TAMM4", "TOYB3", "TOYB4", "TENE5", "TENE7",
        "TCSA3", "TCNO3", "TCNO4", "TGMA3", "TEKA3", "TEKA4", "TKNO3", "TKNO4", "TNCP3", "TNCP4",
        "TNLP3", "TNLP4", "TELB3", "TELB4", "TLPP3", "TLPP4", "TMAR3", "TMAR5", "TMAR6", "TMCP3",
        "TMCP4", "TMGC3", "TMGC6", "TMGC7", "TMGC11", "TMGC12", "TMGC13", "TLVT3B", "TEMP3", "TRNA3",
        "TRNA4", "TXRX3", "TXRX4", "TCSL3", "TCSL4", "TOTS3", "TPIS3", "TBLE3", "TRFO3", "TRFO4",
        "LUXM3", "LUXM4", "TRIS3", "TROR3", "TROR4", "TUPY3", "TUPY4", "UGPA3", "UGPA4", "UBHD3",
        "UBBR3", "UBBR4", "UNIP3", "UNIP5", "UNIP6", "UOLL4", "UCOP4", "USIM3", "USIM5", "USIM6",
        "VLTR3B", "VPSC3", "VPSC4", "VPTA3", "VPTA4", "VINE3", "VINE5", "VINE6", "VIVO3", "VIVO4",
        "VCPA4", "VULC3", "WEGE3", "WMBY3", "MWET3", "MWET4", "WHRL3", "WHRL4", "WISA3", "WISA4",
        "SGAS3", "SGAS4", "ILMD3", "ILMD4"
    };

    /** Creates new form Principal */
    public Principal() {
        initComponents();
        populaAcoes();
        acoes = new LinkedList<String>();
        prospectadores = new ArrayList<Prospectador>();
        investidores = new ArrayList<Investidor>();
        repInd = new ReputacaoIndividual();
        repSoc = new ReputacaoSocial();
        repOnt = new ReputacaoOntologica();
        normaPalpite = new NormaPalpiteErrado();
    }

    public void populaAcoes() {

        DefaultComboBoxModel dados = new DefaultComboBoxModel(cods);
        cbAcoes.setModel(dados);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfNomeProspectador = new javax.swing.JTextField();
        btnInsereProspectador = new javax.swing.JToggleButton();
        cbAcoes = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        tfAdicionarAcao = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tfNomeInvestidor = new javax.swing.JTextField();
        jToggleButton1 = new javax.swing.JToggleButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MITE4O");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Prospectador"));

        jLabel1.setText("Nome");

        btnInsereProspectador.setText("Inserir");
        btnInsereProspectador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsereProspectadorActionPerformed(evt);
            }
        });

        cbAcoes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Ações");

        tfAdicionarAcao.setText("Adicionar");
        tfAdicionarAcao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfAdicionarAcaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tfNomeProspectador, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                        .addGap(111, 111, 111))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnInsereProspectador)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbAcoes, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tfAdicionarAcao)))
                        .addGap(271, 271, 271))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tfNomeProspectador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbAcoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(tfAdicionarAcao))))
                .addGap(29, 29, 29)
                .addComponent(btnInsereProspectador))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Investidor"));

        jLabel2.setText("Nome");

        jToggleButton1.setText("Inserir");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(tfNomeInvestidor, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jToggleButton1)
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfNomeInvestidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButton1))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        jButton1.setText("Simular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(234, 234, 234))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfAdicionarAcaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfAdicionarAcaoActionPerformed
        // TODO add your handling code here:

        acoes.add(cbAcoes.getSelectedItem().toString());
        System.out.println(cbAcoes.getSelectedItem().toString());

    }//GEN-LAST:event_tfAdicionarAcaoActionPerformed

    private void btnInsereProspectadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsereProspectadorActionPerformed
        // TODO add your handling code here:
        prospectadores.add(new Prospectador(reputacaoPadrao,
                acoes,
                new ArrayList<DadosAcao>(),
                criterioPadrao,
                tfNomeProspectador.getText()));
        System.out.println("Foi inserido o agente " + tfNomeProspectador.getText() + " com as seguintes ações:");
        for (String string : acoes) {
            System.out.println(string);
        }
        acoes = new ArrayList<String>();

    }//GEN-LAST:event_btnInsereProspectadorActionPerformed

    //criando agente investidor
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        investidores.add(new Investidor(capital, prospectadores, tfNomeInvestidor.getText()));
        System.out.println("Agente Investidor " + tfNomeInvestidor.getText());
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        tempoInicial = System.currentTimeMillis();

        //atualiza os prospectadores para os agentes investidores



        for (int i = 0; i < 10; i++) {
            for (Investidor inves : investidores) {
                inves.setProspectadores(prospectadores);

            }
            //atualiza o histórico com a cotação atual do conjunto de ações
            for (Prospectador prop : prospectadores) {
                prop.atualizaHistorico();
            }

        }




        while (true) {

            for (Investidor inves : investidores) {
                inves.setProspectadores(prospectadores);

            }
            //atualiza o histórico com a cotação atual do conjunto de ações
            for (Prospectador prop : prospectadores) {
                prop.atualizaHistorico();
            }

            //calcula o palpite que será passado ao investidor
            //armazena estes palpites para o calculo das reputações
            for (Prospectador prop : prospectadores) {
                prop.calculaPalpite();

                for (String acao : prop.getPalpite()) {
                    Palpite pal = new Palpite(prop.getNome(), acao);
                    auxRep.add(pal);
                }

            }

            //ordenar prospectadores por reputação
            for (Investidor inv : investidores) {
                for (Prospectador prop : prospectadores) {
                    //calcula a reputação ontológica
                    if ((repInd.getReputacao().size() > 1) && (repSoc.getReputacao().size() > 1)) {
                        repOnt.atualiza(prop.getNome(), inv.getNome(), reputacaoPro, repInd, repSoc);
                        repOnt.ordena();
                    }
                }
            }


            //investidor escolhe as melhores ações e investe
            int investidas = 0;
            for (Investidor inv : investidores) {
                if (investidas >= 10) {
                    break;
                }

                for (DadosReputacaoOntologica dro : repOnt.getReputacaoOntologica()) {
                    if (investidas >= 10) {
                        break;
                    }
                    //utiliza os dados de reputação se possível
                    if (dro != null) {
                        if (dro.getAgenteAvaliador().equals(inv.getNome()) && (Math.random() * 100 > 50)) {
                            for (Palpite p : auxRep) {
                                if (p.getAgente().equals(dro.getAgenteAlvo())) {
                                    inv.addCarteira(p);
                                    investidas++;
                                    break;
                                }
                            }

                        }
                    } //não utiliza os dados da reputação
                    else {
                        for (Palpite p : auxRep) {

                            inv.addCarteira(p);
                            investidas++;

                            if (investidas >= 10) {
                                break;
                            }
                        }
                    }

                }

                inv.investir();

            }


            //atualiza o histórico com a cotação atual do conjunto de ações
            for (Prospectador prop : prospectadores) {
                prop.atualizaHistorico();
            }

            //calcula reputação dos agentes com base nos palpites e vai atualizando reputação individual e social

            DadosReputacaoIndividual dri;

            for (Investidor inv : investidores) {
                for (Prospectador pro : prospectadores) {
                    for (String acao : pro.getAcoes()) {

                        if (normaPalpite.violou(pro)) {
                            //altera a reputação do prospectador negatvamente
                            //se ainda não existe a reputação insere uma padrao
                            if (repInd.reputacoesIndividual(pro.getNome(), inv.getNome(), reputacaoPro) == null) {
                                dri =
                                        new DadosReputacaoIndividual(reputacaoPro,
                                        reputacaoPadrao,
                                        pro.getNome(),
                                        inv.getNome(),
                                        System.currentTimeMillis());
                                repInd.atualiza(dri);
                            } else {

                                dri = repInd.reputacoesIndividual(pro.getNome(), inv.getNome(), reputacaoPro);
                                dri.setValor(normaPalpite.calculaReputacaoNegativa(dri.getValor()));
                                dri.setTempo(System.currentTimeMillis());
                                repInd.atualiza(dri);
                            }


                            //altera a reputação do investidor positivamente
                            if (repInd.reputacoesIndividual(inv.getNome(), pro.getNome(), reputacaoInv) == null) {
                                dri =
                                        new DadosReputacaoIndividual(reputacaoInv,
                                        reputacaoPadrao,
                                        inv.getNome(),
                                        pro.getNome(),
                                        System.currentTimeMillis());
                                repInd.atualiza(dri);
                            } else {

                                dri = repInd.reputacoesIndividual(inv.getNome(), pro.getNome(), reputacaoInv);
                                dri.setValor(normaPalpite.calculaReputacaoPositiva(dri.getValor()));
                                dri.setTempo(System.currentTimeMillis());
                                repInd.atualiza(dri);
                            }


                        } else {
                            //altera a reputação do prospectador positivamente
                            if (repInd.reputacoesIndividual(pro.getNome(), inv.getNome(), reputacaoPro) == null) {
                                dri =
                                        new DadosReputacaoIndividual(reputacaoPro,
                                        reputacaoPadrao,
                                        pro.getNome(),
                                        inv.getNome(),
                                        System.currentTimeMillis());
                                repInd.atualiza(dri);
                            } else {

                                dri = repInd.reputacoesIndividual(pro.getNome(), inv.getNome(), reputacaoPro);
                                dri.setValor(normaPalpite.calculaReputacaoPositiva(dri.getValor()));
                                dri.setTempo(System.currentTimeMillis());
                                repInd.atualiza(dri);
                            }

                            //altera a reputação do investidor negativamente
                            if (repInd.reputacoesIndividual(inv.getNome(), pro.getNome(), reputacaoInv) == null) {
                                dri =
                                        new DadosReputacaoIndividual(reputacaoInv,
                                        reputacaoPadrao,
                                        inv.getNome(),
                                        pro.getNome(),
                                        System.currentTimeMillis());
                                repInd.atualiza(dri);
                            } else {


                                dri = repInd.reputacoesIndividual(inv.getNome(), pro.getNome(), reputacaoInv);
                                dri.setValor(normaPalpite.calculaReputacaoNegativa(dri.getValor()));
                                dri.setTempo(System.currentTimeMillis());
                                repInd.atualiza(dri);
                            }
                        }



                    }

                }
            }

            repSoc.atualiza(repInd.getReputacao());

            boolean primeiro = true;
            for (Investidor inv : investidores) {
                for (Prospectador pro : prospectadores) {

                    if (repInd.reputacoesIndividual(pro.getNome(), inv.getNome(), reputacaoPro).getValor() < 0.2) {
                        if (primeiro) {
                            pro.setCriterio(((pro.getCriterio() + 1) % 4) + 1);
                            for (Investidor inve : investidores) {
                                repInd.atualiza(new DadosReputacaoIndividual(reputacaoPro, 0.5, pro.getNome(), inve.getNome(), System.currentTimeMillis()));
                            }
                            System.out.println("Mundando critério de " + pro.getNome());

                        }

                    }
                }
                primeiro = false;
            }
            System.out.println("rodou");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }//GEN-LAST:event_jButton1ActionPerformed
    /*Informações das ações*/

    public static String[] getCods() {
        return cods;
    }

    public static void setCods(String[] cods) {
        Principal.cods = cods;
    }


    /*fim dad informações das ações*/
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Principal().setVisible(true);


            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnInsereProspectador;
    private javax.swing.JComboBox cbAcoes;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JButton tfAdicionarAcao;
    private javax.swing.JTextField tfNomeInvestidor;
    private javax.swing.JTextField tfNomeProspectador;
    // End of variables declaration//GEN-END:variables
}
